<?php

namespace App\Http\Controllers\Api\V1\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class OrderResource extends JsonResource
{
	public function toArray($request)
	{
		$resource = $this->resource;
		
		return [
			'id'                        => (int)$resource->id,
			'user_id'                   => (int)$resource->user_id,
			'sid'                   => (int)$resource->sid,
			'is_sec'                   => empty($resource->sid) ? '0' : '1' ,
			'type'                      => $resource->type ?? '',
			'number'                    => $resource->number,
			'total_amount'              => $resource->total_amount,
			'address'                   => $resource->address,
			'is_paid'                   => $resource->isPaid(),
			'is_closed'                 => $resource->isClosed(),
			'is_cancelled'              => $resource->isCancelled(),
			'delivery_status'           => $resource->delivery_status ?? '',
			'has_refund'                => (bool)$resource->has_refund,
			'has_commented'             => (bool)$resource->has_commented,
			'paid_method'               =>$resource->paid_method,
			'apples'                    => $resource->apples,
			'created_at'                => (string)$resource->created_at,

			'is_gold_bean'      => (bool)$resource->is_gold_bean,
			'is_integral'      => (bool)$resource->is_integral,
			
			'status'                    => (string)$resource->getStatus(),
			'aftersale_status'          => (string)$resource->getAftersaleStatus(),
			'is_aftersale_available'    => (bool)$resource->isAftersaleAvailable(),
			
			'user'                      => new UserResource($this->whenLoaded('user')),
			'items'                     => OrderItemResource::collection($this->items),
		];
	}
}
